/**
 ******************************************************************************
 * @FilePath: \Hero\Bsp\bsp_usart.c
 * @Brief: 
 * @Date: 2021-01-10 13:55:45
 * @Author: Rio
 ******************************************************************************
 */
#include "bsp_usart.h"
#include "usart.h"
#include "remoteCtrl.h"
#include "STMGood.h"

/********************************** Extern variables ****************************************/
extern DMA_HandleTypeDef            hdma_usart1_rx;
extern DMA_HandleTypeDef 					  hdma_usart3_rx;
extern DMA_HandleTypeDef 					  hdma_usart6_rx;
extern uint8_t visionData_buff[NUC_REC_LEN];
/********************************** Private variables ****************************************/
uint8_t Rx_data;
uint8_t remoteBuff[30] = {0};
uint8_t usart1_rx_buff[50] = {0};
remoteCtrl_t s_rcData;

/*********************************** printf ************************************************/
int fputc(int ch, FILE *f)
{
	while((DEBUG_USART->SR & 0X40)==0);
	DEBUG_USART->DR = (uint8_t) ch;
	return ch;
}
/********************************** Interface function *************************************/
/**
  * @brief       fetch the pointer of the data structure as a const type to avoid the
  *              value changed by non-interruption action by accident
  * @param[in]   none
  * @retval      pointer of the data structure as const type
  * @note        READ ONLY
  */
const remoteCtrl_t *GetRemoteControlPointer(void)
{
    return &s_rcData;
}

/**
  * @brief       Initialization of the remote control parameters
  * @param[in]   none
  * @retval      none
  * @note        this function make all the value of four channels into the mid-value
  *              and the initialization of two swichers can be defined by yourself
  */
void RemoteControlParamInit(void)
{
    s_rcData.rc.ch[0] = RC_CH_VALUE_OFFSET;
    s_rcData.rc.ch[1] = RC_CH_VALUE_OFFSET;
    s_rcData.rc.ch[2] = RC_CH_VALUE_OFFSET;
    s_rcData.rc.ch[3] = RC_CH_VALUE_OFFSET;
    s_rcData.rc.ch[4] = RC_CH_VALUE_OFFSET;
    s_rcData.rc.s[0] = RC_SW_MID;
    s_rcData.rc.s[1] = RC_SW_DOWN;
}

void MY_UART_ENABLE_DMA_IDLE(UART_HandleTypeDef *huart, DMA_HandleTypeDef *hdma, uint8_t *buff, uint8_t bufflen)
{
	HAL_DMA_Start_IT(hdma, (uint32_t)huart->Instance->DR,(uint32_t)buff, bufflen);
	huart->Instance->CR3 |= USART_CR3_DMAR;
	__HAL_UART_ENABLE_IT(huart,UART_IT_IDLE);
	HAL_UART_Receive_DMA(huart, buff, bufflen);
	__HAL_UART_ENABLE_IT(huart,UART_IT_ERR);
}

/**
 * @brief Uart Init functions
 * @param None
 * @return None
 * @attention it must be called if you need to use USART
 */

void MY_UART_INIT(void)
{
	/* dbus uart */
	MY_UART_ENABLE_DMA_IDLE(&REMOTE_RX_USART_HANDLE, &REMOTE_DMA_USART, remoteBuff, RC_FRAME_LENGTH);
	/* debug uart */
	MY_UART_ENABLE_DMA_IDLE(&DEBUG_USART_HANDLE, &DEBUG_DMA_USART, &Rx_data, DEBUG_BUF_LEN);
	/* NUC uart */
	MY_UART_ENABLE_DMA_IDLE(&NUC_USART_HANDLE, &NUC_DMA_USART, visionData_buff, NUC_REC_LEN);
}

/**
 * @brief DBUS Interrupt function
 * @param None
 * @return None
 * @attention Replace huart1 interrupt in stm32f4xx_it.c
 */
void UART_RX_IDLE_IRQ(UART_HandleTypeDef *huart){
	if(huart->Instance == REMOTE_RX_USART)
	{
		if(__HAL_UART_GET_FLAG(&REMOTE_RX_USART_HANDLE,UART_FLAG_IDLE) != RESET){
			__HAL_UART_CLEAR_IDLEFLAG(&REMOTE_RX_USART_HANDLE);		
			HAL_UART_DMAStop(&REMOTE_RX_USART_HANDLE);	
			HAL_UART_Receive_DMA(&REMOTE_RX_USART_HANDLE,remoteBuff,RC_FRAME_LENGTH);
		}
	} 
	if(huart->Instance == DEBUG_USART)
	{
			if(__HAL_UART_GET_FLAG(&DEBUG_USART_HANDLE,UART_FLAG_IDLE) != RESET){
			__HAL_UART_CLEAR_IDLEFLAG(&DEBUG_USART_HANDLE);		
			HAL_UART_DMAStop(&DEBUG_USART_HANDLE);
			HAL_UART_Receive_DMA(&DEBUG_USART_HANDLE,usart1_rx_buff,DEBUG_BUF_LEN);
		}
	}
	if(huart->Instance == NUC_USART)
	{
			if(__HAL_UART_GET_FLAG(&NUC_USART_HANDLE,UART_FLAG_IDLE) != RESET){
			__HAL_UART_CLEAR_IDLEFLAG(&NUC_USART_HANDLE);		
			HAL_UART_DMAStop(&NUC_USART_HANDLE);
			HAL_UART_Receive_DMA(&NUC_USART_HANDLE,visionData_buff,NUC_REC_LEN);
		}
	}
}


